package im.qingji.sqlutil.connection.impl;

import java.sql.Connection;
import java.sql.SQLException;

import com.jolbox.bonecp.BoneCPDataSource;

import im.qingji.sqlutil.connection.Connector;

/**
 * 
 * @author qingji
 *
 */
public class BoneCPDataSourceConnector implements Connector {

	private static BoneCPDataSource ds;
	@Override
	public Connection connect(String url, String user, String password)
			throws SQLException {
		initDataSource(url, user, password);	
		return ds.getConnection();
	}
	private void initDataSource(String url, String user, String password) {
		if(ds == null) {
			BoneCPDataSource ds = new BoneCPDataSource();  // create a new datasource object
			ds.setJdbcUrl(url);		// set the JDBC url
			ds.setUsername(user);				// set the username
			ds.setPassword(password);
			ds.setMinConnectionsPerPartition(5);
			ds.setMaxConnectionsPerPartition(10);
		}
	}
	
	@Override
	public void clear() {
		if(ds != null) {
			ds.close();
		}
	}

}
